package algorithm.sort;

import java.util.Arrays;

/**
 * 快速排序 错误的演示
 * 基准值没有找到，去找了数组最中间的数
 * 比较基准值的方式也不太对，用>= 和 <=去做比较
 *
 */
public class KuaiSuSortError {

  public KuaiSuSortError(int[] arr) {
    this.arr = arr;
  }

  public static void main(String[] args) {
    int[] arr = {5, 3, 3, 32423, 3241};
    KuaiSuSortError kuaiSu = new KuaiSuSortError(arr);

    int mid = kuaiSu.exchange(0, arr.length - 1);
    System.out.println(mid);
    System.out.println(Arrays.toString(arr));
    kuaiSu.exchange(0, mid);
    kuaiSu.exchange(++mid, arr.length - 1);
    System.out.println(Arrays.toString(arr));

  }

  int[] arr;


  private void sort(int low, int hight) {

  }
//  int[] arr = {5, 2, 6, 3, 26, 1};

  //排序
  private int exchange(int low, int hight) {
     int mid = (low + hight) / 2;
    int midvalue = this.arr[mid];
    int l = 0, h = 0;
    while (true) {
      System.out.println(" fisrt");

      //从左往右元素，大于等于mid的元素提取出来
      for (; low <= hight; low++) {
        if (low == hight) return mid; //指针碰到就推出循环
        if (this.arr[low] >= midvalue) {
          l = low;
          break;
        }
      }
      //从右往左元素，小于等于mid的元素提取出来
      for (; hight >= low; hight--) {
        if (low == hight) return mid; //指针碰到就推出循环
        if (this.arr[hight] <= midvalue) {
          h = hight;
          break;
        }
      }
//      交换元素
      int ex = this.arr[l];
      this.arr[l] = this.arr[h];
      this.arr[h] = ex;

      low++;
      hight--;  //指针往前一步
      if (low >= hight) return mid; //指针各自往前一步可能发生错过了 或
    }
  }


}
